package com.agranee.timemanagementsystem.entity;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.apache.log4j.Logger;

import com.agranee.timemanagementsystem.dao.EmployeeDao;
import com.agranee.timemanagementsystem.util.Utility;

@Entity
@Table(name = "employee")
@ManagedBean
@SessionScoped
public class Employee {
	@Column(name = "employeeUserId")
	private String employeeUserId;
	@Column(name = "employeePassword")
	private String password;
	@Column(name = "employeeName")
	private String employeeName;
	@Column(name = "employeeId")
	@Id
	private String employeeId;
	@Column(name = "emailId")
	private String employeeEmailId;

	private static Logger log = Logger.getLogger(Employee.class.getName());

	/*
	 * The following function is used to check whether the entered User Id and
	 * associated Password is correct or not.
	 */

	public String getEmployeeUserId() {
		return employeeUserId;
	}

	public String getEmployeeEmailId() {
		return employeeEmailId;
	}

	public void setEmployeeEmailId(String employeeEmailId) {
		this.employeeEmailId = employeeEmailId;
	}

	public void setEmployeeUserId(String employeeUserId) {
		this.employeeUserId = employeeUserId;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}

	public String loginCheck() {
		EmployeeDao employeeDao = new EmployeeDao();
		Employee employee = employeeDao
				.getEmployeeByUserId(getEmployeeUserId());
		if (employee != null) {
			if (employee.getPassword().equals(this.getPassword())) {
				log.info("User logged : " + employee.getEmployeeName());
				return "loginsuccessful";
			}/*
			 * else{ FacesMessage message = new FacesMessage(
			 * "Login Unsuccessfull Please Enter Correct Password");
			 * FacesContext context = FacesContext.getCurrentInstance();
			 * context.addMessage(UIComponent.getCurrentComponent(context)
			 * .getClientId(), message);
			 * 
			 * }
			 */

		}
		FacesMessage message = new FacesMessage(
				"Login Unsuccessfull Please Enter Correct UserId and Password !!");
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(UIComponent.getCurrentComponent(context)
				.getClientId(), message);
		return null;

	}

	public void forgotPassword() {
		EmployeeDao employeeDao = new EmployeeDao();

		Employee employee = employeeDao.getEmployeeEmployeeId(this
				.getEmployeeId());
		if (employee != null) {
			String user = "swarnimpr";
			String pswd = "0542190261.";
			String from = "swarnimpr@gmail.com";
			String to = "prashant@agranee.com";
			String subject = "Retriving password";
			String content = "Testing for sending mail";
			System.out.println("Employee : " + employee.getEmployeeName());
			Utility.sendMail(user, pswd, from, to, subject, content);
			log.info("Employee found as : " + employee.getEmployeeEmailId());
		}
		FacesMessage message = new FacesMessage("User Doesn\'t exists !!!");
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(UIComponent.getCurrentComponent(context)
				.getClientId(), message);
	}

	public void forgotUserId() {
		EmployeeDao employeeDao = new EmployeeDao();

		Employee employee = employeeDao.getEmployeeEmployeeId(this
				.getEmployeeId());
		if (employee != null) {
			String user = "swarnimpr";
			String pswd = "0542190261.";
			String from = "swarnimpr@gmail.com";
			String to = "prashant@agranee.com";
			String subject = "Retriving password";
			String content = "Testing for sending mail";
			System.out.println("Employee : " + employee.getEmployeeName());
			Utility.sendMail(user, pswd, from, to, subject, content);
			log.info("Employee found as : " + employee.getEmployeeEmailId());
		}
		FacesMessage message = new FacesMessage("User Doesn\'t exists !!!");
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(UIComponent.getCurrentComponent(context)
				.getClientId(), message);

	}
}